'use strict';

const BaseController = require('../base');
const fs = require('fs');
const path = require('path');
const _ = require('lodash');
const common = require('../../const/common');
const pkgInfo = require('../../../package.json');
const os = require('os');

class PicController extends BaseController {

  async about() {
    const currentPlatform = os.platform();
    let layoutContentHeight = '570px';
    if (currentPlatform === 'darwin') {
      layoutContentHeight = '600px';
    }
    const data = {
      softName: pkgInfo.softName,
      version: pkgInfo.version,
      feedback: '530353222(QQ)',
      qqGroup: '959497956',
      official: 'https://www.yuque.com/u34495/dqt5f5/ghr9wf',
      author: '哆啦好梦',
      softDescription: pkgInfo.description,
      layoutMainContent: {
        background: '#fff',
        minHeight: layoutContentHeight,
      },
    }

    this.sendSuccess(data);
  }

  async uploadFile() {
    const self = this;
    const { ctx, service } = this;
    const body = ctx.request.body;
    const pid = body.pid;
    console.log('pid:', pid);
    const tmpDir = service.pic.getTmpDir();
    const file = ctx.request.files[0];
    //this.app.logger.info('file:', file);

    if (!_.includes(common.providers, pid)) {
      self.sendFail({}, '服务id错误', CODE.SYS_PARAMS_ERROR);
      return;
    }
    
    try {
      let tmpFile = fs.readFileSync(file.filepath) 
      fs.writeFileSync(path.join(tmpDir, file.filename), tmpFile)
    } finally {
      await fs.unlink(file.filepath, function(){});
    }
    
    const uploadRes = await service.pic.uploadFile(pid, file);
    if (uploadRes.code !== CODE.SUCCESS) {
      self.sendFail({}, uploadRes.msg, uploadRes.code);
      return;
    }

    const data = uploadRes.data;
    self.sendSuccess(data);
  }

  async setConfig() {
    const self = this;
    const { ctx, service } = this;
    const body = ctx.request.body;

    const result = await service.pic.setConfig(body);

    self.sendSuccess(result);
  }

  async getConfig() {
    const self = this;
    const { ctx, service } = this;
    const body = ctx.request.body;

    const data = await service.pic.getConfig(body.pid);
    self.sendSuccess(data);
  }

  async getFileList() {
    const self = this;
    const { ctx, service } = this;
    const body = ctx.request.body;
    const pid = body.pid || null;
    const page = Number(body.page || 1);
    const perPage = Number(body.perPage || 10);
    const content = body.content;
    if (!_.includes(common.providers, pid)) {
      self.sendFail({}, '服务id错误', CODE.SYS_PARAMS_ERROR);
      return;
    }

    const list = await service.pic.getFileList(pid, page, perPage, content);
    const count = await service.storage.getFileCount(pid, content);
    const data = {
      page: page,
      totalCount: Number(count),
      list: list
    }

    self.sendSuccess(data);
  }

  async FileDelete() {
    const self = this;
    const { ctx, service } = this;
    const body = ctx.request.body;
    const pid = body.pid;
    const fileId = body.fileId;
    if (_.isEmpty(fileId)) {
      self.sendFail({}, 'id不存在', CODE.SYS_PARAMS_ERROR);
      return;
    }
    const delRes = await service.pic.delFile(pid, fileId);
    if (_.isEmpty(delRes)) {
      self.sendFail({}, '文件不存在', CODE.SYS_PARAMS_ERROR);
      return;
    }

    self.sendSuccess({});
  }

  async exportData() {
    const self = this;
    const { service } = this;

    await service.pic.exportData();
    // if (_.isEmpty(exportRes)) {
    //   self.sendFail({}, 'ss', CODE.SYS_PARAMS_ERROR);
    //   return;
    // }

    self.sendSuccess({}, '备份成功，正在打开...');
  }

  async importData() {
    const self = this;
    const { ctx, service } = this;
    const tmpDir = service.pic.getTmpDir();
    const file = ctx.request.files[0];
    const tmpFilePath = path.join(tmpDir, file.filename);
    try {
      let tmpFile = fs.readFileSync(file.filepath) 
      fs.writeFileSync(tmpFilePath, tmpFile)
    } finally {
      await fs.unlink(file.filepath, function(){});
    }

    await service.pic.importData(tmpFilePath);

    self.sendSuccess({}, '导入成功');
  }
}

module.exports = PicController;
